
let carouseid;
layui.config({
}).use(['form','laydate', 'layer', 'element','upload'],function(){
	form = layui.form;
	layer = layui.layer;
    laydate = layui.laydate;//日期
    upload = layui.upload;//上传
    element = layui.element; //元素操作
	carouseid = GetQueryString("carouseid");	//广告ID
	if(!isNotNull(carouseid)){
		carouseid = 0;
		$("#title_name").html("添加轮播图");
	}else{
		$("#title_name").html("修改轮播图");
		initdatas();
	}
	//普通图片上传
	let uploadInst = upload.render({
		elem: '#uploadimgs'
		,url: httpurl+'/uploadImgQiniu?pathName=advert&divisor=7&dividend=5'
		,before: function(obj){
			let flag = true;
		  //预读本地文件示例，不支持ie8
		  obj.preview(function(index, file, result){
			$("#divpreview").css("display","block");
			$('#preview').attr('src', result); //图片链接（base64）
		  });
		},done: function(res){
			// console.log(JSON.stringify(res)+"=======done");
			if(res.rtnCode==500){//如果上传失败
				$("#divpreview").css("display","none");
				$('#preview').attr('src', ""); //图片链接（base64）
				return layer.msg(res.msg);
			}
		  $("#preview").attr("name",res.msg);
		},error: function(err){
		  //演示失败状态，并实现重传
			let demoText = $('#demoText');
			demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn  demo-reload">重试</a>');
			demoText.find('.demo-reload').on('click', function(){
			uploadInst.upload();
		  });
		}
	 });
});

//查看广告详情
function initdatas(){
	//加载
	var index = top.layer.msg('查询中，请稍候',{icon: 16,time:false,shade:0.8});
	$.ajax({
		url: httpurl+"/findById",
		cache: false,
		data: {
			"carouseid":carouseid
		},
		// dataType: "json",
		success: function(data) {
			console.log("查看广告详情====="+JSON.stringify(data));
			top.layer.closeAll();	 //关闭所有
			let dataList=data.data.CarouseInfo;
			$("#adv_position option[value="+dataList.type+"]").attr("selected",true);
			if(!(dataList.picurl=="")){
				$("#preview").attr("src",visitimgurl+"/"+dataList.picurl);
				$("#preview").attr("name",dataList.picurl);
				$("#divpreview").show();
			}
			$("#adv_url").val(dataList.target)
			$("#stort").val(dataList.sort);
		}
	});
}

//添加或修改
function add_adv(){
	let selectid=$("#adv_position option:selected").val();
	if(selectid==-1){
		layer.msg("请选择轮播位置！",{icon:2});
		return;
	}
	let imgs=$("#preview").attr("name");
	if(!isNotNull(imgs)){
		layer.msg("请选择轮播图片！",{icon:2});
		return;
	}
	let targetUrl=$("#targetUrl").val();
	if(targetUrl==""){
		layer.msg("请输入跳转链接！",{icon:2});
		return;
	}

	let stort=$("#stort").val();
	if(stort==""){
		stort=0;
	}
    top.layer.confirm('是否确认提交吗？',{icon: 3,title:'提示信息'},function(index){
		var index = top.layer.msg('提交中，请稍候',{icon: 16,time:false,shade:0.8});
		setTimeout(function(){
			let ajaxurl="/UpdateCarouseById";
			if(carouseid==0){
				ajaxurl="/InsertCarouse";
			}
			$.ajax({
				type:"POST",
				url: httpurl+ajaxurl,
				cache: false,
				data: JSON.stringify({
					"id":carouseid,
					"type":selectid,
					"picurl":imgs,
					"target":targetUrl,
					"sort":stort,
				}),
				dataType: "json",
				headers: {
					"Access-Control-Allow-Origin": "*",
					"Access-Control-Allow-Methods": "*",
					"Access-Control-Allow-Headers": "Authorization",
					"Authorization": GetTokenInfo()
				},
				contentType : 'application/json',
				success: function(data) {
					top.layer.closeAll();	 //关闭所有
					top.layer.msg("修改成功!!");
					re();
				},error:function (res) {
					top.layer.msg("修改失败，请稍后再试!");
				}
			});
		},1000);
	})
}
//返回
function re(){
	window.location.href="NoticeList.html";
}
